// pages/utilPage/pages/lockList/lockList.js
const app = getApp();
const util = require('../../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lockList: [],
    pageIndex: 1,
    pageSize: 20,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let self = this;
    self.getGroundLockList(false);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    let self=this;
    self.setData({
      pageIndex:1,
      lockList:[]
    })
    self.getGroundLockList(true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let self = this;
    self.setData({
      pageIndex: self.data.pageIndex++
    })

    self.getGroundLockList(false);

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  /**
   * 获取地锁列表
   */
  getGroundLockList: function(isCon) {
    let self = this;
    let url = app.globalData.apiRootUrl + '/CarLock/GetLocks';
    app.httpRequest({
      url: url,
      data: {
        userId: app.globalData.userInfo.UserID,
        pageIndex: self.data.pageIndex,
        pageSize: self.data.pageSize
      },
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0 && rdata.data.rows.length) {
          if(isCon){
            self.setData({
              lockList: self.data.lockList.concat(...rdata.data.rows)
            })
          }else{
            self.setData({
              lockList:rdata.data.rows
            })
          }
         
        }
      },
      fail: function(err) {

      }
    })
  },
  //返回
  goBack: function() {
    let self = this;
    wx.redirectTo({
      url: '/pages/utilPage/pages/groundLock/groundLock'
    })
  },
  //编辑锁
  lockEdit: function(e) {
    let self = this;
    let id = e.currentTarget.dataset.lockid;
    let query = "?lockid=" + id;
    wx.navigateTo({
      url: '/pages/utilPage/pages/addGroundLock/addGroundLock' + query,
    })
  },
  //删除锁
  lockDelet: function(e) {
    let self = this;
    let id = e.currentTarget.dataset.lockid;
    console.log(id);
    let url = app.globalData.apiRootUrl + '/CarLock/DeleteLock';
    app.httpRequest({
      url: url,
      data: {
        ID: id
      },
      method: 'POST',
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0) {
          let list = self.data.lockList;
          for (let i = 0; i < list.length; i++) {
            if (list[i].ID == id) {
              list.splice(i, 1);
              self.setData({
                lockList: list
              })
            }
          }
          wx.showToast({
            title: '删除成功'
          })
        }
      },
      fail: function(err) {
        wx.showToast({
          title: '删除失败',
          icon: 'none'
        })
      }
    })
  },
  //前往添加地锁
  goToAddLock: function() {
    let self = this;
    wx.navigateTo({
      url: '/pages/utilPage/pages/addGroundLock/addGroundLock',
    })
  }
})